package com.dlc.shop.seckill.api.listener;

import com.dlc.shop.bean.event.SeckillOrderStockEvent;
import com.dlc.shop.bean.vo.StockPointSkuVO;
import com.dlc.shop.common.bo.MultiDecrementBO;
import com.dlc.shop.common.enums.MultiStockEnum;
import com.dlc.shop.manager.impl.StockManager;
import com.dlc.shop.seckill.common.service.SeckillSkuService;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 支付时校验订单过期事件
 * @author lhd
 */
@Component("secKillOrderStockListener")
@AllArgsConstructor
public class SecKillOrderStockListener {
    private final SeckillSkuService seckillSkuService;
    private final StockManager stockManager;

    @EventListener(SeckillOrderStockEvent.class)
    public void checkSecKillOrderHandle(SeckillOrderStockEvent event) {
        List<StockPointSkuVO> stockPointSkuList = event.getStockPointSkuList();
        List<MultiDecrementBO> multiDecrementList = new ArrayList<>(stockPointSkuList.size());
        for (StockPointSkuVO stockPointSkuVO : stockPointSkuList) {
            MultiDecrementBO decrementBO = new MultiDecrementBO(MultiStockEnum.SECKILL, null, null,
                    event.getSeckillId(), stockPointSkuVO.getSkuId(), null, null, null);
            decrementBO.setStockPointId(stockPointSkuVO.getStockPointId());
            multiDecrementList.add(decrementBO);
        }
        Map<String, Integer> stockMap = stockManager.listSeckillStock(multiDecrementList);
        event.setStockMap(stockMap);
    }

}
